package com.exercise;

import java.io.FileReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

public class Ptools {
	public void xmlParser5(Reader reader) {
		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();
			parser.setInput(reader);
			
			int type = parser.getEventType();
			while (type != 1) {
				String tag = parser.getName();
				switch (type) {
				case 2:
					if (tag.equals("wuhan")) {
						p(parser.getAttributeValue(0));
					} else if (tag.equals("city")) {
						for (int i = 0; i < 18; i++) {
							System.out.print(parser.getAttributeValue(i)+" ");
						}
						p("\n");
					} 
					break;
				default:
					break;
				}
				type=parser.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void xmlParser4(String path) {
		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();
			parser.setInput(new FileReader(path));
			
			int type = parser.getEventType();
			while (type != 1) {
				String tag = parser.getName();
				switch (type) {
				case 2:
					if (tag.equals("wuhan")) {
						p(parser.getAttributeValue(0));
					} else if (tag.equals("city")) {
						for (int i = 0; i < 18; i++) {
							System.out.print(parser.getAttributeValue(i)+" ");
						}
						p("\n");
					} 
					break;
				default:
					break;
				}
				type=parser.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void xmlParser3(String path) {
		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();
			parser.setInput(new FileReader(path));
			int type = parser.getEventType();
			while (type != 1) {
				String tag = parser.getName();
				switch (type) {
				case 2:
					if (tag.equals("root")) {
						p(parser.getAttributeValue(0));
					} else if (tag.equals("province")) {
						p(parser.getAttributeValue(0)+"    "+parser.getAttributeValue(1));
					} else if (tag.equals("city")) {
						p(parser.getAttributeValue(0)+"    "+parser.getAttributeValue(1));
					} else if (tag.equals("area")) {
						p(parser.getAttributeValue(0)+"    "+parser.getAttributeValue(1));
					} 
					break;
				default:
					break;
				}
				type=parser.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	static void p(Object o){
		System.out.println(o);
	}
	public List<Book> xmlParser(String path) {
		List<Book> list = null;
		Book book = null;
		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();
			parser.setInput(new FileReader(path));
			int type = parser.getEventType();
			while (type != 1) {
				String tag = parser.getName();
				switch (type) {
				case 0:
					list = new ArrayList<Book>();
					break;
				case 2:
					if (tag.equals("book")) {
						book = new Book();
						book.setCategory(parser.getAttributeValue(0));
					} else if (tag.equals("title")) {
						book.setLang(parser.getAttributeValue(0));
						book.setTitle(parser.nextText());
					} else if (tag.equals("author")) {
						book.setAuthor(parser.nextText());
					} else if (tag.equals("year")) {
						book.setYear(parser.nextText());
					} else if (tag.equals("price")) {
						book.setPrice(Double.parseDouble(parser.nextText()));
					}
					break;
				case 3:
					if (tag.equals("book"))
						list.add(book);
				default:
					break;
				}
				type = parser.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public List<Note> xmlParser2(String path) {
		List<Note> list = null;
		Note note = null;
		StringBuffer sb = null;
		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();
			parser.setInput(new FileReader(path));
			int type = parser.getEventType();
			while (type != 1) {
				String tag = parser.getName();
				switch (type) {
				case 0:
					list = new ArrayList<Note>();
					break;
				case 2:
					if (tag.equals("note")) {
						note = new Note();
					} else if (tag.equals("from")) {
						note.setFromSB(parser.nextText());
					} else if (tag.equals("to")) {
						note.setToSB(parser.nextText());
					} else if (tag.equals("message")) {
						note.setMsg(parser.nextText());
					} else if (tag.equals("date")) {
						sb = new StringBuffer();
					} else if (tag.equals("year")) {
						sb.append(parser.nextText() + "-");
					} else if (tag.equals("month")) {
						sb.append(parser.nextText() + "-");
					} else if (tag.equals("day")) {
						sb.append(parser.nextText());
					}
					break;
				case 3:
					if (tag.equals("note"))
						list.add(note);
					else if (tag.equals("date"))
						note.setDate(sb.toString());
				default:
					break;
				}
				type = parser.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
